package cn.cangfei.massage.controller;


import cn.cangfei.massage.common.api.CommonResult;
import cn.cangfei.massage.dto.param.VisitProductParamDto;
import cn.cangfei.massage.dto.param.VisitProductQueryParam;
import cn.cangfei.massage.dto.update.VisitProductInfo;
import cn.cangfei.massage.service.IVisitProductService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.security.Principal;

/**
 * @author Lemon  2313812755@qq.com
 * @date 2023/2/12
 */
@Api(value = "预约产品模块", tags = "预约产品接口")
@RestController
@RequestMapping("/visit")
@CrossOrigin
public class VisitProductController {

    @Resource
    private IVisitProductService visitProductService;

    @ApiOperation(value = "添加预约产品信息", notes = "传入 预约参数对象（产品编号、账号编号）")
    @RequestMapping(value = "/appointment/product", method = RequestMethod.GET)
    public CommonResult insertAppointmentProduct(Principal principal, @RequestParam("productId") Long productId, @RequestParam("buycount") Integer buycount) {

        String openId = principal.getName();
        if (visitProductService.countProductId(productId)) {
            return CommonResult.validateFailed("不存在，该产品！");
        }
        String insert = visitProductService.insert(openId, productId, buycount);
        if ("0".equals(insert)) {
            return CommonResult.success();
        }
        if ("1".equals(insert)) {
            return CommonResult.validateFailed("已经预约过了");
        }

        return CommonResult.failed();
    }

    @ApiOperation(value = "修改预约的数量", notes = "传入 ")
    @RequestMapping(value = "/appointment/update/product", method = RequestMethod.GET)
    public CommonResult updateAppointmentProduct(Principal principal, @RequestParam("productId") Long productId, @RequestParam("buycount") Integer buycount) {

        String openId = principal.getName();
        if (visitProductService.countProductId(productId)) {
            return CommonResult.validateFailed("不存在，该产品！");
        }
        Boolean result = visitProductService.updateAppointmentProduct(openId, productId, buycount);
        if (result) {
            return CommonResult.success();
        }

        return CommonResult.failed();
    }

    @ApiOperation(value = "批量删除预约记录", notes = "传入 你要删除的类型id")
    @RequestMapping(value = "/appointment/delete/visitProduct", method = RequestMethod.DELETE)
    public CommonResult deleteAppointmentProduct(@RequestParam("type") Integer type) {

        //
        if (visitProductService.deleteAppointmentProducts(type)) {
            return CommonResult.success();
        }
        return CommonResult.failed();
    }

    @ApiOperation(value = "删除预约产品信息", notes = "传入 预约参数对象（产品编号、账号编号）")
    @RequestMapping(value = "/appointment/product/delete/{id}", method = RequestMethod.POST)
    public CommonResult deleteAppointmentProduct(@PathVariable("id") Long id) {

        if (visitProductService.countId(id)) {
            return CommonResult.validateFailed("不存在该预约信息！");
        }

        if (visitProductService.deleteAppointmentProduct(id)) {
            return CommonResult.success();
        }
        return CommonResult.failed();
    }


    @ApiOperation(value = "完成预约产品", notes = "传入 预约id参数")
    @RequestMapping(value = "/appointment/product/ok/{id}", method = RequestMethod.POST)
    public CommonResult AppointmentProductOk(@PathVariable("id") Long id) {
        // 判断是否存在
        if (visitProductService.countId(id)) {
            return CommonResult.validateFailed("该预约信息不存在");
        }

        if (visitProductService.AppointmentProductOk(id)) {
            return CommonResult.success();
        }
        return CommonResult.failed();
    }

    @ApiOperation(value = "取消预约产品", notes = "传入 预约id参数")
    @RequestMapping(value = "/appointment/product/cancel/{id}", method = RequestMethod.POST)
    public CommonResult AppointmentProductCancel(@PathVariable("id") Long id) {
        // 判断是否存在
        if (visitProductService.countId(id)) {
            return CommonResult.validateFailed("该预约信息不存在");
        }

        if (visitProductService.AppointmentProductCancel(id)) {
            return CommonResult.success();
        }
        return CommonResult.failed();
    }

    @ApiOperation(value = "查询预约产品详细信息", notes = "传入 预约参数对象（预约编号）")
    @RequestMapping(value = "/appointment/product/get/{id}", method = RequestMethod.GET)
    public CommonResult<VisitProductInfo> getAppointmentProductInfo(@PathVariable("id") Long id) {

        if (visitProductService.countId(id)) {
            return CommonResult.validateFailed("不存在该预约！");
        }

        VisitProductInfo visitProductInfo = visitProductService.getAppointmentProductInfo(id);

        return CommonResult.success(visitProductInfo);
    }


    @ApiOperation(value = "搜索 预约产品信息", notes = "传入 预约参数对象（预约编号）")
    @RequestMapping(value = "/appointment/product/getList", method = RequestMethod.GET)
    public CommonResult<IPage<VisitProductParamDto>> getAppointmentListProductInfo(@RequestParam(required = false) String productName,
                                                                                   @RequestParam(required = false) String userName,
                                                                                   @RequestParam("pageNum") Integer pageNum,
                                                                                   @RequestParam("pageSize") Integer pageSize) {

        return CommonResult.success(visitProductService.list(productName, userName, pageNum, pageSize));


    }

    @ApiOperation(value = "小程序端查看自己的预约产品信息", notes = "传入 预约参数对象（自己的userId就可以了）")
    @RequestMapping(value = "/appointment/product/getListByUserId", method = RequestMethod.GET)
    public CommonResult<IPage<VisitProductParamDto>> getAppointmentListProductInfoByUserId(Principal principal) {

        // openId
        String openId = principal.getName();
        return CommonResult.success(visitProductService.getAppointmentListProductInfoByUserId(openId));


    }

    @ApiOperation(value = "管理端端查看所有预约产品信息", notes = "传入 预约参数对象（自己的userId就可以了）")
    @RequestMapping(value = "/appointment/product/getList", method = RequestMethod.POST)
    public CommonResult<IPage<VisitProductParamDto>> getAppointmentListProductInfo(@RequestBody VisitProductQueryParam param,
                                                                                   @RequestParam("pageNum") Integer pageNum,
                                                                                   @RequestParam("pageSize") Integer pageSize) {
        IPage<VisitProductParamDto> paramDtoIPage = visitProductService.getAppointmentListProductInfo(param, pageNum, pageSize);
        return CommonResult.success(paramDtoIPage);

    }


}
